//
//  GoodsListViewController.m
//  share_business
//
//  Created by 吴于凌 on 2017/9/10.
//  Copyright © 2017年 wyl. All rights reserved.
//

#import "GoodsListViewController.h"
#import "GoodsListHeaderView.h"
#import "GoodsListItemCell.h"
#import "GoodsDetailViewController.h"
@interface GoodsListViewController ()<UITextFieldDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)GoodsListHeaderView *headerView ;

@property (nonatomic, strong) BaseCollectionview *collectionView;
@property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout ;

@end

@implementation GoodsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.headerView = [[GoodsListHeaderView alloc]initWithFrame:CGRectMake(0, 0, APPWIDTH, 50)];
    [self.view addSubview:self.headerView];
    [self initCollectionView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
 }

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setTopTitleView];
    [self topLeftItemButton:ImageNamed(@"back") title:nil];
    [self topRightItemButton:ImageNamed(@"home_message") image2:nil title1:@"消息" title2:nil];
}

-(void)setTopTitleView{
    UIView *titleViewM = [[UIView alloc]initWithFrame:CGRectMake(0, 27, APPWIDTH - 72*2, 35)];
    UIImageView *searchView = [BSWidget commonImageViewWithFrame:CGRectMake(10, 0, 17, 18) image:ImageNamed(@"home_search_tip")];
    searchView.centerY = 35 / 2 ;
    
    UITextField *searchBar = [[UITextField alloc] init];
    searchBar.delegate = self;
    searchBar.returnKeyType = UIReturnKeySearch;
    searchBar.frame = CGRectMake(searchView.maxX +3, 0, APPWIDTH - 72*2, 35);
    searchBar.placeholder = @" 搜索全部";
    searchBar.font =[UIFont systemFontOfSize:14];
    searchBar.backgroundColor = [UIColor clearColor];
    [titleViewM setBorder:0 cornerRadius:4];
    
    titleViewM.backgroundColor= [UIColor colorWithHexString:COLOR_LIGHT_GRAY];
    [titleViewM addSubview:searchBar];
    [titleViewM addSubview:searchView];
    self.navigationItem.titleView = titleViewM;
    
}


-(void)initCollectionView{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _collectionView = [[BaseCollectionview alloc]initWithFrame:CGRectMake(0, 50, APPWIDTH, APPHEIGHT) collectionViewLayout:self.flowLayout];
    _collectionView.backgroundColor =[UIColor colorWithHexString:COLOR_LIGHT_GRAY];
    _collectionView.showsVerticalScrollIndicator =  NO;
    //定义每个UICollectionView 横向的间距
    self.flowLayout.minimumLineSpacing = 0;
    //定义每个UICollectionView 纵向的间距
    self.flowLayout.minimumInteritemSpacing = 0;
    //定义每个UICollectionView 的边距距
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0, -0.25, 0, -0.25);//上左下右
    
    //注册cell和ReusableView（相当于头部）
    [_collectionView registerClass:[GoodsListItemCell class] forCellWithReuseIdentifier:@"GoodsListItemCell"];
   
    //设置代理
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    //自适应大小
    _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    [self.view addSubview:self.collectionView];
}



#pragma mark UICollectionView相关
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
 
    return 10 ;
}

//每个UICollectionView大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    //最新活动
  
        return CGSizeMake(APPWIDTH/2, 260) ;
  
}
#pragma mark 定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(0, 0);
}
#pragma mark 每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
 
    static NSString *identify = @"GoodsListItemCell";
    GoodsListItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    return cell;
 
}


#pragma mark UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GoodsDetailViewController *vc = [[GoodsDetailViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES ;
    [self.navigationController pushViewController:vc animated:YES];
}

//左边按钮
-(void)leftAction:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
